﻿#region License
/*

	Copyright (C) 2008 Oliver Charles, Daniel Hollander

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Moebius.Rendering;

namespace Moebius.Core
{
    /// <summary>
    /// An abstract way to create specific resources
    /// </summary>
    public sealed class ResourceFactory
    {
        readonly Dictionary<string, ResourceManager> resourceManagers = new Dictionary<string, ResourceManager>();
        private ILogger logger;
      
        public ILogger Logger
        {
	    get { return logger; }
	    set { logger = value; }
	}

        #region Load Resource

        /// <summary>
        /// Load a resource from the file system.
        /// </summary>
        /// <typeparam name="TResourceType">The type of resource to load.</typeparam>
        /// <param name="path">The path to the resource, on the users file system.</param>
        /// <returns>An instance of the requested resource.</returns>
        public TResourceType Load<TResourceType>(string path)
        {
            if (!Path.HasExtension(path)) throw new ArgumentException("path must have an extension", "path");
            if (!File.Exists(path))
                throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, "The file at {0} could not be found", path));

            string extension = Path.GetExtension(path);

            if (resourceManagers.ContainsKey(extension))
            {
	        if(logger != null)
		    Logger.LogInfo(string.Format("Loading {0}. Expecting a {1} back.", path, typeof(TResourceType)));

                ResourceManager manager = resourceManagers[extension];
                return (TResourceType) manager.LoadResource(path, this);
            }
            else
                return default(TResourceType);
        }

        #endregion
        #region Register Resource Manager

        /// <summary>
        /// Register a <see cref="ResourceManager"/> with this <see cref="ResourceFactory"/>, for a set of specific file extensions
        /// </summary>
        /// <param name="extensions">A list of all filename extensions that this resource manager will handle.</param>
        /// <param name="resourceManager">A <see cref="ResourceManager"/> instance that will load the resources.</param>
        public void RegisterResourceManager(string[] extensions, ResourceManager resourceManager)
        {
            foreach(string extension in extensions)
                resourceManagers.Add(extension, resourceManager);
        }

        #endregion
    }
}